﻿using Microsoft.AspNetCore.Mvc;
using System.Data;
using Microsoft.Data.Sqlite;
using Dapper;

namespace Cherry.Controllers
{
    [ApiController]
    public class UserController : ControllerBase
    {
        [HttpGet("getUsers")]
        public Models.JSONResult GetUsers()
        {
            //Response.Headers["Content-Type"] = "application/json";
            Models.JSONResult result = new Models.JSONResult {
            Success=false,
            Code=0,
            };

            result.Data= Enumerable.Range(0, 10).Select(index => new Models.User
            {
                ID = index,
                Name = $"User{index}",
                Password = BCrypt.Net.BCrypt.EnhancedHashPassword($"User{index}")
            }) ;
            result.Success = true;
            result.Code = 1;

            return result;
        }



        /// <summary>
        /// <parametes>ID</parametes>
        /// <parametes>password</parametes>
        /// </summary>
        /// <returns></returns>
        [HttpPost("login")]
        public Models.JSONResult Login()
        {
            Models.JSONResult result = new Models.JSONResult {
                Success = false,
                Code = 0,
            };
            int id = int.Parse(Request.Form["ID"]);
            string password=Request.Form["Password"];

            //按照提供的ID从数据库读取
            string crypt_password = "";

            if(BCrypt.Net.BCrypt.EnhancedVerify(password,crypt_password))
            {
                result.Success = true;
                result.Code = 1;
            }


            return result;
        }

        [HttpDelete("delete")]
        public Models.JSONResult Delete()
        {
            Models.JSONResult result = new Models.JSONResult { Success = false, Code = 0 };

            //调用数据库进行相应操作

            return result;


        }


        private int Add(Models.User u)
        {
            using (IDbConnection conn = new SqliteConnection(Models.Options.ConnectionString))
            {
                return conn.Execute("insert into user() values()", u);
            }
        }



    }
}
